from django.views import View
from django.http import JsonResponse

from apps.tea.models import ChaJu, ChaShi, TeaSpace, Home, TypeTea


# 获取茶具信息
class TeaSet(View):
    def get(self, request):
        tea_sets = []
        # 获取茶具所有信息
        try:
            tea_set = ChaJu.objects.all()
            print(tea_set)
        except Exception as error:
            print(error)
            return JsonResponse({"code": 400, "errmsg": "获取茶具信息数据失败"})
        for tea in tea_set:
            tea_sets.append({"id": tea.id, "name": tea.name, "apply": tea.apply, "technology": tea.technology,
                             "brief_history": tea.brief_history})
        print(tea_sets)
        return JsonResponse({"code": 200, "errmsg": "OK", "tea_set": tea_sets})


# 获取茶叶历史信息
class TeaHistory(View):
    def get(self, request):
        tea_historys = []
        # 获取茶史所有信息
        try:
            tea_history = ChaShi.objects.all()
            print(tea_history)
        except Exception as error:
            print(error)
            return JsonResponse({"code": 400, "errmsg": "获取茶史信息数据失败"})
        for tea in tea_history:
            tea_historys.append({"id": tea.id, "time": tea.time, "synopsis": tea.synopsis, "dynasty": tea.dynasty,
                                 "tea_history": tea.history_img})
        print(tea_historys)
        return JsonResponse({'code': 200, 'errmsg': 'OK', "tea_history": tea_historys})


# 获取茶席信息
class TeaMat(View):
    def get(self, request):
        tea_spaces = []
        # 获取茶席信息
        try:
            tea_space = TeaSpace.objects.all()
            print(tea_space)
        except Exception as error:
            print(error)
            return JsonResponse({"code": 400, "errmsg": "获取茶席信息失败"})
        for tea in tea_space:
            tea_spaces.append(
                {"id": tea.id, "headline": tea.headline, "textContent": tea.textContent, "image": tea.image})
        print(tea_spaces)
        return JsonResponse({"code": 200, "errmsg": "OK", "tea_space": tea_spaces})


# 获取茶叶信息
class Tea(View):
    def get(self, request):
        try:
            tea_list = Home.objects.all()
            tea_type = TypeTea.objects.all()
        except Exception as error:
            print(error)
            return JsonResponse({"code": 400, "errmsg": "获取茶叶信息失败"})
        tea_names = []
        for teas in tea_type:
            tea_names.append({"id": teas.id, "tea_name": teas.tea_name, "children": []})
            for tea in tea_list:
                if teas.id == tea.tea_type_id_id:
                    tea_names[teas.id - 1]['children'].append(
                        {"id": tea.id, "tea_chinese": tea.tea_chinese, "tea_origin": tea.tea_origin,
                         "tea_effect": tea.tea_effect, "tea_storage": tea.tea_storage,
                         "tea_fabrication": tea.tea_fabrication, "tea_place": tea.tea_place,
                         "tea_maketeastep": tea.tea_maketeastep, "tea_image": tea.tea_image})
        return JsonResponse({"code": 200, "errmsg": "OK", "tea": tea_names})
